package com.lex.crossgate.admin.modules.sys.service.imp;

import com.lex.crossgate.common.constants.Constant;
import com.lex.crossgate.common.constants.R;;
import com.lex.crossgate.admin.modules.sys.entity.SysRoleMenu;
import com.lex.crossgate.admin.modules.sys.service.SysRoleMenuService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.lex.crossgate.admin.modules.sys.dao.SysRoleMenuDao;
import java.util.List;

/**
 * java类简单作用描述
 *
 * @ProjectName: crossgate
 * @Package: com.lex.crossgate.admin.modules.sys.service.imp
 * @ClassName: ${TYPE_NAME}
 * @Description: java类作用描述
 * @Author: 李贝留斯
 * @CreateDate: 2018/5/2 15:55
 * @Version: 1.0
 **/
@Service("sysRoleMenuService")
public class SysRoleMenuServiceImpl implements SysRoleMenuService {

    @Autowired
    private SysRoleMenuDao sysRoleMenuDao;
    /**
     * @description: 保存权限
     * @author 李贝留斯
     * @date 2018/5/17 20:33
     */
    @Override
    @Transactional
    public R save(String menuId,String roleId) {
        //删除次用户权限
        EntityWrapper entityWrapper = new EntityWrapper();
        entityWrapper.eq("role_id",roleId);
        sysRoleMenuDao.delete(entityWrapper);


        //重新保存用户权限
        int rows = 0;
            String[] menuIds = menuId.split(",");
            for (String mId : menuIds){
                    SysRoleMenu sysUserRole = new SysRoleMenu();
                    sysUserRole.setRoleId(roleId);
                    sysUserRole.setMenuId(mId);
                    int row = sysRoleMenuDao.insert(sysUserRole);
                    rows+= row;
            }
        return R.ok(Constant.ERROR);

    }

    public List<SysRoleMenu> getRoleMenuById(String roleId) {
        EntityWrapper entityWrapper = new EntityWrapper();
        entityWrapper.eq("role_id",roleId);
        return sysRoleMenuDao.selectList(entityWrapper);
    }
}
